﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Xml;
using Common.Utilities;
using Workflows.Components.Rules.Properties;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// set global variable
    /// </summary>
    [Serializable]
    public class RuleEffect:RuleNodeDataBase
    {
        

        private FieldGetter _Getter;
        /// <summary></summary>
        public FieldGetter Getter
        {
            get
            {
                return this._Getter;
            }
            set
            {
                this._Getter = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public RuleEffect()
        {
            this.Getter = new FieldGetter();
            this.Name = "Effect";
            this.Description = string.Empty;
            this.Img = Resources.Task;
            this.RuleNodeType = Rules.RuleNodeType.Effect;
        }

        public override System.Xml.XmlNode Serialize(ref System.Xml.XmlDocument xDoc, ref System.Xml.XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldName", this.Getter.FieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldDataType", ((int) this.Getter.FieldDataType).ToString());
            return xNode;
            

        }

        public override RuleNodeDataBase Instantiate(XmlNode xNode)
        {
            RuleEffect ruleEffect=new RuleEffect();
            ruleEffect.RuleNodeID=new Guid(xNode.Attributes.GetNamedItem("RuleNodeID").Value);
            ruleEffect.Name = xNode.Attributes.GetNamedItem("Name").Value;
            ruleEffect.Description = xNode.Attributes.GetNamedItem("Description").Value;
            ruleEffect.RuleNodeType = (RuleNodeType) int.Parse(xNode.Attributes.GetNamedItem("RuleNodeType").Value);
            FieldGetter getter = new FieldGetter();
            getter.FieldName = xNode.Attributes.GetNamedItem("FieldName").Value;
            getter.FieldDataType = (SimpleDataType)int.Parse(xNode.Attributes.GetNamedItem("FieldDataType").Value);
            ruleEffect.Getter = getter;
            return ruleEffect;
        }

        public override string DescribeInHtml()
        {
            StringBuilder buffer = new StringBuilder();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(new StringWriter(buffer));
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.B);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.U);
            htmlWriter.Write(string.Format("Output {0}:", this.RuleNodeID.ToString()));
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Br);
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
            if (this.Getter != null)
                htmlWriter.Write(string.Format("Get field: {0}", this.Getter.FieldName));
            else 
                htmlWriter.Write("Field getter is not initialized");
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            return buffer.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string Describe()
        {
            StringBuilder buffer=new StringBuilder();
            buffer.Append("Effect: " + this.RuleNodeID.ToString());
            buffer.Append(Environment.NewLine);
            if(this.Getter !=null)
            {
                buffer.Append("Get field " + this.Getter.FieldName == null
                                  ? "unknown field name"
                                  : this.Getter.FieldName);
            }
            else
            {
                buffer.Append("Getter is not initialized");
            }
            buffer.Append(Environment.NewLine);
            return buffer.ToString();
        }

    }
}
